"""
Case Type   : spq多机并行
Case Name   : alter system set 设置comm_sctp_port参数,取非法值合理报错
Create At   : 2023/12/15
Owner       : songjing20
Description :
    1.查看comm_sctp_port的值，查询pg_settings系统表
    2.设置为0、1、65535、7000、数据库端口值
    3.设置为非法值
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.合理报错
    4.成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SpqPlugin(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'comm_sctp_port'
        self.values = ['0', '1', '65535', '7000', self.pri_node.db_port]
        self.err_values = ['-1', '65536', '38700.5', 'none', '*', '$', '/']

    def test_spq(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:查看comm_sctp_port的值;expect:成功-----'
        self.log.info(text)
        self.para_value = self.pri_sh.execut_db_sql(
            f"show {self.param};").splitlines()[-2].strip()
        self.log.info(self.para_value)
        self.assertEqual(self.para_value, str(int(self.pri_node.db_port) + 30),
                         f'执行失败:{text}')

        text = '---step2:设置为0、1、65535、7000、数据库端口;expect:成功---'
        self.log.info(text)
        for value in self.values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execut_db_sql(
                f"alter system set {self.param} = {value};")
            self.log.info(res)
            self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, res,
                          f'执行失败:{text}')
            self.pri_sh.restart_db_cluster()
            res = self.common.show_param(self.param)
            self.assertEqual(res, str(value), f'执行失败:{text}')

        text = '-----step3:设置为非法值;expect:报错-----'
        self.log.info(text)
        for value in self.err_values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execut_db_sql(
                f"alter system set {self.param} = {value};")
            self.log.info(res)
            self.assertNotIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, res,
                             f'执行失败:{text}')
            self.assertIn('ERROR', res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"alter system set {self.param} = {self.para_value};")
        self.log.info(res)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, res,
                      f'执行失败:{text}')
        self.pri_sh.restart_db_cluster()
        res = self.common.show_param(self.param)
        self.assertEqual(res, str(self.para_value), f'执行失败:{text}')
        self.log.info(f'----{os.path.basename(__file__)} end-----')
